#include <math.h>
#include "gaevaluator.h"
#include <memory.h>


class nev : public GAEvaluator
{
public:
    nev(){}
    
    void Randomize(unsigned char* _g, long _iLen)
    {
        double* data = (double*) _g;
        long mLen = _iLen/8;

        for(long i = 0 ; i < mLen;i++)
        {
            data[i++] = ((rand()%10000-5000.0)/1000.0f);
        }
    }
    
    void Mutate(unsigned char* _g, long _iLen, float _fRatio, long _iAmount)
    {
        double *data = (double*) _g;
        *(data + (rand() % 2)) += (rand() % 2) ? 0.001 : -0.001;
    }
    
    float evaluate_single(float* input, unsigned char* _g, long _iLen)
    {
        float fAv = 0;
    
        double* data = (double*) _g;
        double x = data[0];
        double y = data[1];
        double fVal = 1.0f + cos(x/1.3)*sin(y/1.3)*exp(-x*x/50)*exp(-y*y/50);
        fAv = fVal;
        return fAv;
    }

		void print(unsigned char* _g, long _iLen)
		{
			double* data = (double*) _g;
			long mLen = _iLen/8;
			for(int i = 0 ; i < mLen; i++) 
			{
				printf("%2.2f", data[i]);
				if (i < mLen-1)
				{
					printf(" ");
				}
			}
		}

    int getSize() {return 16; }
    
    int getPop() {return 10;}
    
    float getTarget() { return 0.006f; }
    
    float getMutationRatio() { return 1.0f; }
    
    void toFile(unsigned char* _g, long _lSize, const char* file)
		{
			FILE *f = NULL;
			f = fopen(file, "w");
			double *farr = (double*) _g;

			for (int i = 0 ; i < _lSize/8; i++)
			{
				fprintf(f, "%2.2f ", farr[i]);
			}

			fclose(f);
    }
};

extern "C" GAEvaluator* dimga_get_evaluator(){ return new nev();}


